PyVistaで pythreejs を使う¶
pythreejs jupyterlab plotting backend は、 threejs を利用してウェブベースの可視化を可能にする強力なライブラリです。また、(ここに示されているように)htmlドキュメントを埋め込むことができます。
他のバックエンドと比較して pythreejs バックエンドを使用する最大の利点は、VTKのシーンを正確に threejs のシーンに再現してくれることです。
メッシュエッジ
照明
物理ベースのレンダリング
面と点のスカラ
質感
このバックエンドを使用して、Jupyterノートブック内でPyVistaシーンを直接表示したり、インタラクティブなWebドキュメントを作成したり、スタンドアロンのHTMLページにエクスポートすることもできます。
注釈
このバックエンドは ipygany バックエンドよりも優れたサポートと機能を持っていますが、vtk ウィジェットのサポートやいくつかの機能 (スカラーバーやラベルなど) はまだありません。 詳しくは 注意点 をご覧ください。
PyVistaラッピング¶
pyvistaのプロットシーンは、 pythreejs バックエンドを使用すると、自動的にthree.jsのシーンにシリアライズされます。 これは pyvista.set_jupyter_backend() でグローバルに有効にすることも、 pyvista.Plotter.show() で設定することもできます。
import pyvista as pv
from pyvista import examples
mesh = examples.download_bunny()
mesh.flip_normals()
pl = pv.Plotter()
pl.add_mesh(mesh, color='lightgrey')
pl.camera_position = 'xy'
pl.show(jupyter_backend='pythreejs')
メッシュカラー,バックグラウンドカラー,カメラ位置がすべて three.js シーンにマップされることに注意してください.つまり,既存のコードを再利用して,使用するプロットバックエンドのタイプに応じてバックエンドを変更できます.
すべてのvtkウィジェットを含む多くの機能が欠けていることに注意してください,しかし,これらの多くはjupyterlabウィジェットで置き換えることができます.独自のシーンをアセンブルする場合は,次のように "viewer" を返しながらjupyter_backendを変更します.
>>> pl = pv.Plotter()
>>> pl.add_mesh(mesh, color='lightgrey')
>>> pl.background_color = 'white'
>>> pl.camera_position = 'xy'
>>> widget = pl.show(jupyter_backend='pythreejs', return_viewer=True)
>>> type(widget)
pythreejs.core.Renderer.Renderer
このレンダラーは、いくつものjupyterlabウィジェットに追加して,完全なウィジェットとして表示することができます.例えば, ipywidgets.AppLayout を使用して2つを並べて表示することもできます.
プロットの表現と素材¶
PyVistaの作図シーンは、three.jsの中で、同じ照明、カメラの投影、素材を使って、同じ作図シーンに忠実にシリアライズされます。
# set the global theme to use pythreejs
pyvista.global_theme.jupyter_backend = 'pythreejs'
pl = pyvista.Plotter()
# lower left, using physically based rendering
pl.add_mesh(pyvista.Sphere(center=(-1, 0, -1)),
show_edges=False, pbr=True, color='white', roughness=0.2,
metallic=0.5)
# upper right, matches default pyvista plotting
pl.add_mesh(pyvista.Sphere(center=(1, 0, 1)))
# Upper left, mesh displayed as points
pl.add_mesh(pyvista.Sphere(center=(-1, 0, 1)),
color='k', style='points', point_size=10)
# mesh in lower right with flat shading
pl.add_mesh(pyvista.Sphere(center=(1, 0, -1)), lighting=False,
show_edges=True)
# show mesh in the center with a red wireframe
pl.add_mesh(pyvista.Sphere(), lighting=True, show_edges=False,
color='red', line_width=0.5, style='wireframe',
opacity=0.99)
pl.camera_position = 'xz'
pl.show()
スカラサポート¶
バックエンドの pythreejs は、面のスカラーや、点、ワイヤーフレーム、面の表現のためのポイントのプロットをサポートしています。
import pyvista
pyvista.global_theme.show_scalar_bar = False
import numpy as np
def make_cube(center=(0, 0, 0), resolution=1):
cube = pyvista.Cube(center=center)
return cube.triangulate().subdivide(resolution)
pl = pyvista.Plotter()
# test face scalars with no lighting
mesh = make_cube(center=(-1, 0, -1))
mesh['scalars_a'] = np.arange(mesh.n_faces)
pl.add_mesh(mesh, lighting=False, cmap='jet', show_edges=True)
# test point scalars on a surface mesh
mesh = make_cube(center=(1, 0, 1))
mesh['scalars_b'] = mesh.points[:, 2]*mesh.points[:, 0]
pl.add_mesh(mesh, cmap='bwr', line_width=1)
mesh = make_cube(center=(-1, 0, 1))
mesh['scalars_c'] = mesh.points[:, 2]
pl.add_mesh(mesh, style='points', point_size=30)
# test wireframe
mesh = make_cube(center=(1, 0, -1))
mesh['scalars_d'] = mesh.points[:, 2]
pl.add_mesh(mesh, show_edges=False, line_width=3,
style='wireframe', cmap='inferno')
pl.camera_position = 'xz'
pl.show()
点群の例¶
pyvistaのバックエンドである pythreejs を用いて、点のスカラーにランダムな値を割り当てながら、サンプルの点群をプロットします。
pc = pyvista.PolyData(np.random.random((100, 3)))
pc['scalars'] = np.random.random(100)
pc.plot(jupyter_backend='pythreejs', style='points', point_size=10, cmap='jet')
質感¶
また、 pythreejs バックエンドでは、 textures をサポートしています。
import pyvista
globe = examples.load_globe()
globe.plot(jupyter_backend='pythreejs', smooth_shading=True)
テクスチャに関する詳細は、 テクスチャを適用する の例を参照してください。
RGBとRGBAのカラーリング¶
pythreejs は RGBA プロットをサポートしています。 詳細は add_mesh() の rgba パラメータを参照してください。
import numpy as np
import pyvista
mesh = pyvista.Sphere()
# treat the points as RGB coordinates to make a colorful mesh
pts = mesh.points.copy()
pts -= pts.min()
rgba_sphere = (255*pts).astype(np.uint8)
# plot the corners for fun
corners = mesh.outline_corners()
pts = corners.points.copy()
pts -= pts.min()
pts = 255*(pts/pts.max()) # Make 0-255 RGBA values
corners['rgba_values'] = pts.astype(np.uint8)
edges = corners.tube(radius=0.01).triangulate()
pl = pyvista.Plotter(window_size=(600, 600))
pl.add_mesh(mesh, scalars=rgba_sphere, rgba=True, smooth_shading=True)
pl.add_mesh(edges, rgba=True, smooth_shading=True)
pl.show(jupyter_backend='pythreejs')
複数のレンダリングウィンドウ¶
1つの pythreejs の中で、PyVista と同じように、複数のレンダリングウィンドウをプロットすることができます。
例として、 スカラーをメッシュに割り当てる を参照してください。
大規模モデルと物理ベースのレンダリング¶
この例では,大きなメッシュを示し,500,000 万個の面と 250,000 個の点を含むCarburetorの例など非常に大きなメッシュでもすばやくロードできることを示します.このメッシュは約6 MBであるため,これは帯域幅に依存します.
なお、ここでは pbr=True を使って物理ベースのレンダリングを有効にしています。
import pyvista as pv
from pyvista import examples
pv.set_jupyter_backend('pythreejs')
# download an example and reduce the mesh density
mesh = examples.download_carburator()
mesh.decimate(0.5, inplace=True)
# Plot it on a white background with a lightgrey mesh color. Enable
# physically based rendering and give the mesh a metallic look.
mesh.plot(window_size=(600, 600), background='w', color='lightgrey',
pbr=True, metallic=0.5)
インタラクティブなドキュメントの作成¶
このページに掲載されているすべてのドキュメントは、 pythreejs, pyvista, jupyter_sphinx の組み合わせで作成されています。
例えば、サンプルの *.rst ファイルでは、次のように追加します。
.. jupyter-execute::
import pyvista as pv
from pyvista import examples
pv.set_jupyter_backend('pythreejs')
mesh = pv.Cube()
mesh.plot(show_edges=True)
生成するには
また, :hide-code: オプションを使うと,コードを隠してプロットだけを表示することができます。
また、プロット時にグローバルテーマを変更することで、プロットの見栄えを良くすることができます。
import pyvista
pyvista.global_theme.background = 'white'
pyvista.global_theme.window_size = [600, 600]
pyvista.global_theme.antialiasing = True
以下のパッケージが必要になります。
pyvistapythreejsjupyter_sphinx
conf.py に, 次を追加します.
extensions = [
"jupyter_sphinx",
# all your other extensions
]
HTMLへのエクスポート¶
pythreejs を使うと、ほとんどのシーンを完全にスタンドアローンのHTMLファイルに書き出すことができます。 例えば、以下のようになります。
>>> import pyvista
>>> from pyvista import examples
>>> mesh = examples.load_uniform()
>>> pl = pyvista.Plotter(shape=(1,2))
>>> _ = pl.add_mesh(mesh, scalars='Spatial Point Data', show_edges=True)
>>> pl.subplot(0,1)
>>> _ = pl.add_mesh(mesh, scalars='Spatial Cell Data', show_edges=True)
>>> pl.export_html('pyvista.html')
注意点¶
現在、PyVista のすべての機能が pythreejs プロットバックエンドでサポートされているわけではありません。将来の機能を追加するには、 PyVista Issues にある機能リクエストを開いてください。
不足している機能は以下の通りです。